Delphi code for getting/setting formulas

The code below assumes that a PrintJob is already open.

Calls used
PEGetNFormulas
PEGetNthFormula
PESetNthFormula
PEGetFormula
PESetFormula
PECheckFormula
PEGetHandleString
Code
uses CRDelphi;

var
  slFormulas : TStringList;

{In this example, the Formula Names and Texts are stored to slFormulas, a 
public StringList}
procedure GetFormulas;
var
  NameHandle  : hWnd;
  NameLength  : smallint;
  TxtHandle   : hWnd;
  TxtLength   : smallint;
  TxtBuffer   : PChar;
  nFormulas   : smallint;
  nFormula    : smallint;
begin
  {Get number of Formulas}
  nFormulas := PEGetNFormulas(PrintJob);
  if (nFormulas = -1) then
    {Do Error Handler};

  slFormulas := TStringList.Create;

  {Loop through the formulas}
  for nFormula := 0 to (nFormulas - 1) do
  begin
    NameLength := 0;
    TxtLength := 0;
    {Retrieve formula}
    if not PEGetNthFormula(PrintJob, nFormula, NameHandle, NameLength, 
TxtHandle, TxtLength) then
      {Do Error Handler};

    {Get Formula Name}
    TxtBuffer := StrAlloc(NameLength);
    if not PEGetHandleString(NameHandle, TxtBuffer, NameLength) then
      {Do Error Handler};

    {Add a new Formula Name to StringList}
    slFormulas.Add(StrPas(TxtBuffer));

    {Get Formula Text}
    StrDispose(TxtBuffer);
    TxtBuffer := StrAlloc(TxtLength);
    if not PEGetHandleString(TxtHandle, TxtBuffer, TxtLength) then
      {Do Error Handler};
    
    {Add the Formula Text to the StringList Object}
    slFormulas.Objects[nFormula] := TStringList.Create;
    TStringList(slFormulas.Objects[nFormula]).SetText(TxtBuffer);
    StrDispose(TxtBuffer);
  end;
end;


{This procedure assumes we are using the public slFormulas StringList 
which was filled with Formula information in the GetFormulas procedure 
above}
procedure SetFormulas;
var
  sFormulaName : string;
  nTextLen     : smallint;
  hText        : hWnd;
  nFormulas    : smallint;
  nFormula     : smallint;
  pRptFormula  : PChar;
  Changed      : boolean;
  slTemp       : TStringList;
begin
  {Get the number of Formulas}
  nFormulas := PEGetNFormulas(PrintJob);
  if (nFormulas = -1) then
    {Do Error Handler};

  {Loop through the formulas}
  for nFormula := 0 to (nFormulas - 1) do
  begin
    Changed := False;

    {Get the Formula Name from the StringList created in GetFormulas}
    sFormulaName := slFormulas[nFormula];
    nTextLen := 0;

    {Retrieve the Formula currently in the Report}
    if not PEGetFormula(PrintJob, PChar(sFormulaName), hText, nTextLen) 
then
      {Do Error Handler};

    {Get the Formula Text from the Report}
    pRptFormula := StrAlloc(nTextLen);
    if not PEGetHandleString(hText, pRptFormula, nTextLen) then
      {Do Error Handler};

    {Put the Report Formula Text into a StringList}
    slTemp := TStringList.Create;
    slTemp.SetText(pRptFormula);

    {Compare it to the new formula...If they are the same, do not send}
    if StrComp(TStringList(slFormulas.Objects[nFormula]).GetText, 
slTemp.GetText) <> 0 then
       Changed := True;
    StrDispose(pRptFormula);
    slTemp.Free;

    {Send the Formula to the Report}
    if Changed then
    begin
      if not PESetFormula(PrintJob, PChar(sFormulaName), 
TStringList(slFormulas.Objects[nFormula]).GetText) then
        {Do Error Handler};

      {Check the Formula}
      if not PECheckFormula(PrintJob, PChar(sFormulaName)) then
        {Do Error Handler};
    end;
  end;
end;


{Here is a procedure which shows how to free the memory allocated for the 
slFormulas StringList}
procedure FreeFormulasList;
var
  cnt    : integer;
  slTemp : TStringList;
begin
  for cnt := (slFormulas.Count - 1) downto 0 do
  begin
    slTemp := TStringList(slFormulas.Objects[cnt]);
    slTemp.Free;
  end;
  slFormulas.Free;
end;


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com